package cm.iccsoft.android.telecomcost.ui;

import cm.iccsoft.android.telecomcost.activities.TelecomcostActivity;
import cm.iccsoft.android.telecomcost.activities.VoiceRateListActivity;
import cm.iccsoft.android.telecomcost.entities.Time;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TimePicker;

public class TimePickerView extends TableRow {

	private Context _context;
	private TextView _label;
	private EditText _txtTime;
	private Button _btnSetTime;
	private TimePickerDialog dialog;
	private Time _time;

	public Time getTime() {
		return _time;
	}

	public void setTime(Time _time) {
		this._time = _time;
	}

	public TimePickerView(Context context, String label) {
		super(context);
		Log.i("TimePickerView", "creating the time picker view for " + label);
		_context = context;
		setDialog();
	}
	

	private void setDialog() {		
		_label = new TextView(_context);
		_txtTime = new EditText(_context);
		_btnSetTime = new Button(_context);		

		_btnSetTime.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				showTimePickerDialog();
			}
		});

		addView(_label);
		addView(_txtTime);
		addView(_btnSetTime);
	}

	public TimePickerView(Context context, AttributeSet attrs) {		
		super(context, attrs);
		Log.i("TimePickerView", "creating the time picker view with AttributeSet");
		
		_context = context;
		setDialog();
	}

	public TimePickerView(Context context) {
		super(context);
		Log.i("TimePickerView", "creating the time picker view");
		_context = context;
		setDialog();
	}

	protected void showTimePickerDialog() {
		dialog = new TimePickerDialog(_context,mTimeSetListener,0,0,true);
		dialog.show();
	}
	

	// the callback received when the user "sets" the time in the dialog
	private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			_time = new Time(hourOfDay, minute);
			_txtTime.setText(_time.toString());
		}
	};
}
